//
//  RGMainpageSearchVIew.m
//  RedGrass
//
//  Created by wukong on 2018/12/11.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGMainpageSearchVIew.h"

@interface RGMainpageSearchVIew()

@property (strong, nonatomic) UIImageView *searchIcon;
@property (strong, nonatomic) UITextField *searchTextField;
@property (strong, nonatomic) UIButton *closeBtn;

@end


@implementation RGMainpageSearchVIew


-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = UICOLOR_WITH_RGBINT(0x160f2e);
        ViewRadius(self, 18.5);
        [self initUI];
        [self addMasonry];
    }
    return self;
}

-(void) initUI{
    [self addSubview:self.searchIcon];
    [self addSubview:self.searchTextField];
    [self addSubview:self.closeBtn];
}

-(void)addMasonry{
    [_searchIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self).mas_offset(0);
        make.left.mas_equalTo(self).mas_offset(18);
        make.size.mas_equalTo(CGSizeMake(18, 18));
    }];
    
    [_searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self).mas_offset(0);
        make.left.mas_equalTo(self.searchIcon.mas_right).mas_offset(8);
        make.right.mas_equalTo(self.closeBtn.mas_left).mas_offset(-8);
        make.height.mas_equalTo(30);
    }];
    
    [_closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self).mas_offset(0);
        make.right.mas_equalTo(self).mas_offset(-10);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
}


#pragma mark - public

-(CGSize) viewSize{
    return CGSizeMake(KScreenWidth - 80, 37);
}

#pragma mark - reponse

-(void)closeBtnClick:(UIButton *)button{
    
    if (self.hidden == NO) {
        self.alpha = 1.0;
        [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^ {
            self.alpha = 0.0;
            NSLog(@"out animate start");
        }completion:^(BOOL finished) {
            NSLog(@"out animate completion");
            self.hidden = YES;
        }];
    }
}

#pragma mark - getter
-(UIImageView *)searchIcon{
    if(!_searchIcon){
        _searchIcon = [[UIImageView alloc]init];
        _searchIcon.image = IMAGE_NAMED(@"形状16");
    }
    return _searchIcon;
}

-(UITextField *)searchTextField{
    if(!_searchTextField){
        _searchTextField = [[UITextField alloc]init];
        _searchTextField.placeholder = @"搜索用户、视频名称";
        _searchTextField.font = FONT(customFontName, 12);
        _searchTextField.textColor = UICOLOR_WITH_RGBINT(0x938f9d);
        _searchTextField.tintColor = UICOLOR_WITH_RGBINT(0xF43556);
    }
    return _searchTextField;
}

-(UIButton *)closeBtn{
    if(!_closeBtn){
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeBtn setImage:IMAGE_NAMED(@"形状17") forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(closeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

@end
